package com.zhongen.oa.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongen.common.core.util.R;
import com.zhongen.common.log.annotation.SysLog;
import com.zhongen.oa.entity.ProjectTaskBugEntity;
import com.zhongen.oa.service.ProjectTaskBugService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * bug表
 *
 * @author Code Generator
 * @date 2024-04-16 16:46:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/projectTaskBug")
@Tag(description = "projectTaskBug", name = "bug表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProjectTaskBugController {

	private final ProjectTaskBugService projectTaskBugService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param projectTaskBug bug表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('oa_projectTaskBug_view')")
	public R getProjectTaskBugPage(@ParameterObject Page page, @ParameterObject ProjectTaskBugEntity projectTaskBug) {
		return R.ok(projectTaskBugService.pageList(page, projectTaskBug));
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param projectTaskBug bug表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/pageApp")
	public R getProjectTaskBugPageApp(@ParameterObject Page page, @ParameterObject ProjectTaskBugEntity projectTaskBug) {
		return R.ok(projectTaskBugService.pageListApp(page, projectTaskBug));
	}

	/**
	 * 通过id查询bug表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('oa_projectTaskBug_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(projectTaskBugService.getById(id));
	}

	/**
	 * 新增bug表
	 * @param projectTaskBug bug表
	 * @return R
	 */
	@Operation(summary = "新增bug表", description = "新增bug表")
	@SysLog("新增bug表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('oa_projectTaskBug_add')")
	public R save(@RequestBody ProjectTaskBugEntity projectTaskBug) {
		return R.ok(projectTaskBugService.save(projectTaskBug));
	}

	/**
	 * 修改bug表
	 * @param projectTaskBug bug表
	 * @return R
	 */
	@Operation(summary = "修改bug表", description = "修改bug表")
	@SysLog("修改bug表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('oa_projectTaskBug_edit')")
	public R updateById(@RequestBody ProjectTaskBugEntity projectTaskBug) {
		return R.ok(projectTaskBugService.updateById(projectTaskBug));
	}

	/**
	 * 通过id删除bug表
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除bug表", description = "通过id删除bug表")
	@SysLog("通过id删除bug表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('oa_projectTaskBug_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(projectTaskBugService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出excel 表格
	 * @param projectTaskBug 查询条件
	 * @param ids 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('oa_projectTaskBug_export')")
	public List<ProjectTaskBugEntity> export(ProjectTaskBugEntity projectTaskBug, Long[] ids) {
		return projectTaskBugService
			.list(Wrappers.lambdaQuery(projectTaskBug).in(ArrayUtil.isNotEmpty(ids), ProjectTaskBugEntity::getId, ids));
	}

}
